/*
 * Originally coded by Kevin L. Suffecool
 * http://www.suffecool.net/poker/evaluator.html
 * 
 * Improved by Paul Senzee
 * http://www.senzee5.com/2006/06/some-perfect-hash.html
 */

function Evaluator() {
	var deck = initDeck();
	
	this.evaluate = function (c1, c2, c3, c4, c5) {
		return evalHand(deck[c1.getValue()], deck[c2.getValue()],
				deck[c3.getValue()], deck[c4.getValue()], deck[c5.getValue()]);
	}
	
	function initDeck() {
		var tempDeck = new Array(52);
		var n = 0;
		var suit = 0x8000;
		
		for (var i = 0; i < 4; i++, suit >>>= 1) {
			for (var j = 0; j < 13; j++, n++) {
				tempDeck[n] = primes[j] | (j << 8) | suit | (1 << (16 + j));
			}
		}
		
		return tempDeck;
	}
	
	function findFast(u) {
		u += 0xe91aaa35;
		u ^= u >>> 16;
	    u += u << 8;
	    u ^= u >>> 4;
	    var b = (u >>> 8) & 0x1ff;
	    var a = (u + (u << 2)) >>> 19;
	    var r = a ^ hash_adjust[b];
	    return r;
	}
	
	function evalHand(c1, c2, c3, c4, c5) {
		var q = (c1 | c2 | c3 | c4 | c5) >>> 16;
	    
	    if (c1 & c2 & c3 & c4 & c5 & 0xf000) {
	    	return flushes[q];
	    } else {
	    	var s = unique5[q];
	    	if (s !== 0) {
	    		return s;
	    	}
	    }
	    
	    return hash_values[findFast((c1 & 0xff) * (c2 & 0xff) * (c3 & 0xff) * (c4 & 0xff) * (c5 & 0xff))];
	}
}